package cn.uniquesoft.api;

import cn.uniquesoft.service.*;
import cn.uniquesoft.vo.*;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/api/bank")
public class ApiBankController {
    @Value("${resource.pagesize}")
    private int pagesize;
    @Autowired
    private IBankService bankService;
    @Autowired
    private IConsultantService consultantService;
    @Autowired
    private IVisitService visitService;
    @Autowired
    private IConnectService connectService;
    @Autowired
    private ICompanyService companyService;
    @RequestMapping(value = "/getBankList")
    @ResponseBody
    public  List<BankVO>  getBankList(int iid,String option ) {

        List<BankVO> itemList = new ArrayList<>();
        BankVO item = new BankVO();
        item.setIid(iid);
        item.setPageSize(this.pagesize);
        if(option.equals("down")){
            if(iid==0){
                itemList= this.bankService.getItemsFirst(item);
            }else{
                itemList= this.bankService.findListDown(item);
            }
        }else{
            itemList= this.bankService.findListUp(item);
        }
        List<ConsultantVO> clist = new ArrayList<>();
        for(BankVO b:itemList){
            clist= this.consultantService.getItemsByIpid(b.getIid());
            b.setClist(clist);
        }
        // SimplePropertyPreFilter filter = new SimplePropertyPreFilter(BankVO.class, "iid", "ctitle", "curl","dcreator");
        // String str = JSON.toJSONString(itemList);
        return itemList;

    }


    @RequestMapping(value = "/getOneConsultant")
    @ResponseBody
    public  ConsultantVO  getOneConsultant(int iid) {
        ConsultantVO item = this.consultantService.findOneById(iid);
        //获取对接企业（包含对接成功的和正在对接的）
        ConnectVO v = new ConnectVO();
        v.setIconsultant(iid);
        List<ConnectVO> vlist =  this.connectService.findAll(v);
        for(ConnectVO c:vlist){
            String s = this.companyService.findOneById(c.getIcompany()).getCpicpath();
            c.setCcpmpanypic(s);
        }
        item.setVlist(vlist);
        return item;
    }

    @RequestMapping(value = "/getOneBank")
    @ResponseBody
    public  BankVO  getOneBank(int iid) {
        return this.bankService.findOneById(iid);
    }


    @RequestMapping(value = "/getConsultantListByIbank")
    @ResponseBody
    public  List<ConsultantVO>  getConsultantListByIbank(int iid,int ibank) {
        List<ConsultantVO> itemList = new ArrayList<>();
        ConsultantVO item = new ConsultantVO();
        item.setIid(iid);
        item.setPageSize(this.pagesize);
        item.setIbank(ibank);
        itemList = this.consultantService.getConsultantListByIbank(item);
        return itemList;
    }
}
